package com.bgybmi.entity;

import java.util.Date;
import java.util.List;

import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.ActiveRecordException;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Model;

public class MeetCreate extends Model<MeetCreate> {
	private static final long serialVersionUID = 6204222383226990020L;
	static Log log = Log.getLog(MeetCreate.class);	
	public static final MeetCreate me = new MeetCreate();
	//

//	public boolean save(String room,String topic,String zcr,String starttime,
//			String finishtime,String chr,
//			String prechr, String meetbz, String ydr, String device,String isprivate,String isremind){				
//		me.set("room", room);
//		me.set("topic",topic);
//		me.set("starttime", starttime);	
//		me.set("finishtime",finishtime);	
//		System.out.println(finishtime);//null
//		me.set("zcr", zcr);	
//		me.set("chr", chr);
//		me.set("prechr", prechr);
//		me.set("meetbz", meetbz);
//		me.set("ydr", ydr);
//		me.set("device",device);
//		me.set("issend", 0);
//		me.set("status", 0);
//		me.set("isprivate", isprivate);
//		me.set("isremind", isremind);
//		boolean result = false;
//		try{
//			result = me.save();			
//		}catch(ActiveRecordException E){
//			E.printStackTrace();
//		}
//		return result;
//	}
	
	public List<MeetCreate> getAll(String status){
		return me.find("select * from mms_create where status=? order by xh desc",status);
	}
	
	public List<MeetCreate> getAllSend(String sflag){
		return me.find("select * from mms_create where issend=?",sflag);
	}
	
	public boolean setIsSend(int xh,int issend){
		int result = Db.update("update mms_create set issend=? where xh=?", issend,xh);
		return result == 1?true:false;
	}
	/**
	 * 通过序号获取一条记录的详细信息
	 */	
	public MeetCreate findByXh(String xh){		
		return me.findFirst("select * from mms_create where xh=?", xh);
	}
	
	public List<MeetCreate> getAllBusy(){
		return me.find("select xh,place,starttime,finishtime from mms_create where starttime<=NOW() and finishtime>=NOW()");
	}
	
	/**
	 * 得到会议室状态，主持人，其实是发起人列表
	 * @return
	 */
	public List<MeetCreate> getFqr(String xh){
		return me.find("select * from mms_create where finishtime>NOW() and place in(select name from mms_meetingroom where xh=?)", xh);		
	}
	
	public void setStatus(){
		Db.update("update mms_create set status=1 where NOW()>finishtime and status=0");
	}
	
	public int cancelMeeting(String xh,String status){
		int v = Db.update("update mms_create set status=? where xh=?",status,xh);
		return v;
	}
    //2017-11-14
	public List<MeetCreate> findAllByuser(String user,String status){
		System.out.println(user+"--"+status);//status==null?
		return me.find("select * from mms_create where finishtime>NOW() and status=? and (fqr like ? or chr like ?)",status,user,user);
	}
	
	public List<MeetCreate> finByName(String user) {
		// TODO Auto-generated method stub
		return me.find("select * from mms_create where finishtime>NOW() and fqr like %?%",user);
	}

	public List<MeetCreate> finByJoinName(String user) {
		// TODO Auto-generated method stub
		return me.find("select * from mms_create where finishtime>NOW() and chr like %?%",user);
	}

	public List<MeetCreate> findByroom(String room) {
		// TODO Auto-generated method stub
		return me.find("select * from mms_create where place=?",room);
	}

	public List<MeetCreate> findByDate(String date) {
		// TODO Auto-generated method stub
		return me.find("select * from mms_create startime like ?",date);
	}

	public List<MeetCreate> getListByDate(String date, String userid) {
		// TODO Auto-generated method stub
		return me.find("select * from mms_create startime like ? and userid=?",date,userid);
	}

	public List<MeetCreate> getList(String userid) {
		// TODO Auto-generated method stub
		return me.find("select *,starttime,finishtime from mms_create where zcr like ? or chr like ? or ydr like ?","%"+userid+"%","%"+userid+"%","%"+userid+"%");
	}

	public List<MeetCreate> getAllMeetTillNow(Date starttime) {
		// TODO Auto-generated method stub
		return me.find("select * from mms_create where starttime>=?",starttime);
	}

	public List<MeetCreate> getAllByRoom(String room) {
		// TODO Auto-generated method stub
		return me.find("select * from mms_create where room=?",room);
	}

	public MeetCreate getMeetByXh(String xh) {
		// TODO Auto-generated method stub
		return me.findById(xh);
	}

	public  List<MeetCreate> getSendList() {
		// TODO Auto-generated method stub
		return me.find("select * from mms_create where starttime+isremind<current_timestamp() and isremind>0 and sendflag=0");
	}	
	
	public List<MeetCreate> listMeet(String starttime,String finishtime){
		return me.find("select * from mms_create where startime>? and finishtime<?");
	}

	public List<MeetCreate> findByroomAndParam(String xh,String room, String starttime, String finishtime) {
		// TODO Auto-generated method stub
		return me.find("select * from mms_create where xh<>? and room=? and starttime>=? and finishtime<=?",xh,room,starttime,finishtime);
	}

	public List<MeetCreate> findByroomAndParam(String room, String starttime, String finishtime) {
		// TODO Auto-generated method stub
		return me.find("select * from mms_create where room=? and starttime>=? and finishtime<=?",room,starttime,finishtime);
	}

//	public List<Users> findMeetByUsername(String userid) {
//		// TODO Auto-generated method stub
//		return me.find("select * from mms_create");
//	}

//	public List<MeetCreate> findByRelation(String user) {
//		// TODO Auto-generated method stub
//		return me.find("select * from mms_create where finishtime>NOW() and (fqr like %?% or chr like  %?%)",user,user);
//	}

}
